library DeltaUtils initializer init requires LegacySystem


	globals
		private trigger array DeltaTriggers
		private integer DeltaTriggersLength = 0

        private trigger DeltaChapterSpawnTrigger = null
    endglobals
	
    function SetDeltaSpawnEnabled takes boolean b returns nothing
        if b then
            call EnableTrigger(DeltaChapterSpawnTrigger)
        else
            call DisableTrigger(DeltaChapterSpawnTrigger)
        endif
    endfunction
	
	function CreateDeltaTrigger takes nothing returns trigger 
		set DeltaTriggers[DeltaTriggersLength] = CreateTrigger()
		set DeltaTriggersLength = DeltaTriggersLength + 1
		return DeltaTriggers[DeltaTriggersLength - 1]
	endfunction
	
	function CleanUpDeltaTriggers takes nothing returns nothing
		local integer i = 0
		loop
			exitwhen i >= DeltaTriggersLength
			call DisableTrigger(DeltaTriggers[i])
			set i = i + 1
		endloop
	endfunction
	
    private function ComponentVision2 takes nothing returns nothing
        local unit u = CreateUnit(GetEnumPlayer(),'e000',GetLocationX(udg_myloc),GetLocationY(udg_myloc),0.0)
        call UnitApplyTimedLife(u,'Bhwd',10.0)
        set u = null
    endfunction
    
    function M242Drop takes nothing returns nothing
        local item i
        local real x
        local real y
        set udg_myloc = GetRandomLocInRect(udg_PlayerSpawnBoundary)
        set x = GetLocationX(udg_myloc)
        set y = GetLocationY(udg_myloc)
        set udg_CoreObjective = Rect(x-275.0,y-275.0,x+275.0,y+275.0)
        if IsTerrainPathable(x,y,PATHING_TYPE_WALKABILITY) and not(IsTerrainPathable(x,y,PATHING_TYPE_AMPHIBIOUSPATHING)) then
            call DestroyEffect(AddSpecialEffect( "Objects\\Spawnmodels\\Naga\\NagaDeath\\NagaDeath.mdl", x, y ))
        else
            call DestroyEffect(AddSpecialEffect( "Objects\\Spawnmodels\\Undead\\ImpaleTargetDust\\ImpaleTargetDust.mdl", x, y ))
        endif
        set i = CreateItem(udg_M242Components[(udg_CurrentWave-10)],x,y)
        call ALICEText(30.0, "|cfff3ad00ALICE|r : " + GetItemName(i) + " has been airdropped." )
        call PingMinimapEx(x, y, 60.0, 255, 0, 0, false)
        call SetCameraQuickPosition(x,y)
        call ForForce(udg_EchoCompany,function ComponentVision2)
        call PolledWait(2.5)
        call RemoveLocation(udg_myloc)
        set i = null
    endfunction
    
    function ADTCleanup takes nothing returns nothing
        local integer i = 1
        loop
            exitwhen i>4
			
			if udg_AITarget != null and GetWidgetLife(udg_AITarget)>0.405 then
				call UnitAddAbility(udg_AITarget,'Avul')
			endif
			set udg_AITarget = null 
		
            if GetWidgetLife(udg_ADT[i])>0.405 then
                call KillUnit(udg_ADT[i])
            endif
            set i=i+1
        endloop
    endfunction
    
    function DeltaExit takes nothing returns nothing
        local integer i = 1
		call CleanUpDeltaTriggers()
        if GetWidgetLife(udg_DeltaLeader)>0.405 then
            //call DestroyTrigger(gg_trg_Delta_Leader_AI)
        endif
        if udg_DeadDeltas==5 and udg_Chapter==2 then
            call ALICEText(5.0, "|cfff3ad00ALICE|r : 1st SFOD-D squad eliminated." )
        elseif udg_Chapter==2 then
            //call DestroyTrigger(gg_trg_Delta_Force_AI)
            //call DestroyTrigger(gg_trg_Dead_Deltas)
            if udg_CurrentWave!=10 then
                call ALICEText(5.0, "|cfff3ad00ALICE|r : 1st SFOD-D are leaving the perimeter." )
            endif
        endif
        loop
            exitwhen i>5
            call IssuePointOrder(udg_DeltaOperator[i],"move",2400.0,16000.0)
            set i=i+1
        endloop
        call PolledWait(10.)
        set i = 1
        loop
            exitwhen i>5
            if udg_DeltaOperator[i]!= null and GetWidgetLife(udg_DeltaOperator[i])>0.405 then
                call RemoveUnit(udg_DeltaOperator[i])
            endif
            set i=i+1
        endloop
    endfunction

    function DeltaChapterSpawn takes nothing returns nothing
        call GankMarine(Player(13),'n004',2+udg_Spawn,1300,1800)
        call GankMarine(Player(13),'n005',2+udg_Spawn,1300,1800)
        call GankMarine(Player(14),'n00M',1+udg_Spawn,1200,1500)
        call RandomSpawn('n00D',2,GetRandomInt(0,udg_Spawn)) //Crepitus
        call RandomSpawn('u00G',1,GetRandomInt(1,udg_Spawn)) //Wraith #2
        call RandomSpawn('n00K',1,udg_Spawn) //Blood Hound
        call RandomSpawn('nsw1',1,udg_Spawn) //Stalker
        if GetRandomInt(1,2)==1 then
            call RandomSpawn('z002',1,GetRandomInt(0,udg_Spawn)) //Slasher
        else
            call RandomSpawn('n00A',1,GetRandomInt(0, 2)) //Tartarus
        endif
        if GetRandomInt(1,10)==1 then
            call RandomSpawn('uabo',1,1) //Flesh Composite
        endif
        
        call SpawnShredder()

    endfunction

    private function init takes nothing returns nothing	
        set DeltaChapterSpawnTrigger = CreateDeltaTrigger()
        call DisableTrigger(DeltaChapterSpawnTrigger)
        call TriggerRegisterTimerEvent( DeltaChapterSpawnTrigger,50.0,true )
        call TriggerAddAction(DeltaChapterSpawnTrigger,function DeltaChapterSpawn)
    endfunction



endlibrary